package com.mdinic.blogovi.front.page.emailchange;

import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.request.mapper.parameter.PageParameters;
import org.apache.wicket.spring.injection.annot.SpringBean;
import org.apache.wicket.util.string.StringValue;

import com.mdinic.blogovi.front.common.AbstractBasePage;
import com.mdinic.blogovi.front.page.BSession;
import com.mdinic.blogovi.front.page.HomePage;
import com.mdinic.blogovi.model.Person;
import com.mdinic.blogovi.service.PersonService;

/**
 * Class that represents register confirm page.
 * 
 * @author mdinic
 */
public class EmailConfirmPage extends AbstractBasePage {

    private static final String CODE = "code";
    private static final long serialVersionUID = 1L;

    @SpringBean
    private PersonService personService;

    public EmailConfirmPage(PageParameters pageParameters) {
        super(false);

        StringValue code = pageParameters.get(CODE);
        if (code == null) {
            setResponsePage(HomePage.class);
        } else {
            Person person = personService.confirmNewEmail(code.toString());
            if (person == null) {
                add(new Label("message", "Vaša promena nije uspešno relalizovana. Vaš kod za promenu nije važeći."));
            } else {
                add(new Label("message", "Vaša promena e-maila je uspešno relalizovana."));
            }

            BSession.get().setPerson(person);
        }
    }

}
